home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1998 June / SGI Freeware 1998 June.iso / dist / fw_UMINNgopher.idb / usr / freeware / src / gopher_1.12 / gopherd / daemon.c.z / daemon.c
C/C++ Source or Header  |  1997-09-09  |  4KB  |  190 lines

  1. /********************************************************************
  2.  * $Author: drich $
  3.  * $Revision: 1.1 $
  4.  * $Date: 1995/10/03 04:08:23 $
  5.  * $Source: /proj/freeware1.0/gopher1.12/src/gopherd/RCS/daemon.c,v $
  6.  * $Status: $
  7.  *
  8.  * Paul Lindner, University of Minnesota CIS.
  9.  *
  10.  * Copyright 1991, 1992 by the Regents of the University of Minnesota
  11.  * see the file "Copyright" in the distribution for conditions of use.
  12.  *********************************************************************
  13.  * MODULE: daemon.c
  14.  * Routines to detach from and run as a daemon.
  15.  *********************************************************************
  16.  * Revision History:
  17.  * $Log: daemon.c,v $
  18.  * Revision 1.1  1995/10/03  04:08:23  drich
  19.  * gopher 1.2 check-in
  20.  *
  21.  * Revision 1.1  1992/12/10  23:13:27  lindner
  22.  * gopher 1.1 release
  23.  *
  24.  *
  25.  *********************************************************************/
  26.  
  27. #include "gopherd.h"
  28.  
  29. #if defined(_AIX)  /** AIX needs bsd signals.  **/
  30. #define _BSD
  31. #endif
  32.  
  33. /*
  34.  * A BSD style SIGCLD signal handler that can be used by a server
  35.  * that's not interested in its child's exit status, but needs to
  36.  * wait for them, to avoid clogging up the system with zombies.
  37.  *
  38.  * Beware that the calling process may get an interrupted system
  39.  * call when we return, so they had better handle that.
  40.  *
  41.  * (From Stevens pg 82)
  42.  */
  43.  
  44. #include "Wait.h"
  45.  
  46. /* some machines don't have SIGCLD defined */
  47. #ifndef SIGCLD
  48. #define SIGCLD        SIGCHLD
  49. #endif
  50.  
  51. #ifdef hpux
  52. #define TIOCNOTTY       _IO('t', 113)           /* void tty association */
  53. #endif
  54.  
  55. void
  56. sig_child()
  57. {
  58.      /*
  59.       * Use the wait3() system call with the WNOHANG option
  60.       */
  61.  
  62.      int pid;
  63.      Portawait status;
  64.  
  65.      while ( (pid = wait3(&status, WNOHANG|WUNTRACED, NULL)) > 0)
  66.       ;
  67. }
  68.  
  69.  
  70. #include <stdio.h>
  71. #include <sys/param.h>
  72. #include <errno.h>
  73. extern int errno;
  74.  
  75. #if defined(SIGTSTP) && !defined(M_XENIX) && !defined(USG)/* True on a BSD system */
  76. #include <sys/file.h>
  77. #include <sys/ioctl.h>
  78. #endif
  79.  
  80. /*
  81.  * Detach a daemon process from login session context
  82.  */
  83.  
  84. void
  85. daemon_start(ignsigcld)
  86.   int ignsigcld;          /* Nonzero -> nuke zombie children */
  87. {
  88.      register int childpid, fd;
  89.  
  90.      /*
  91.       * If we were started by init (process 1) from the /etc/inittab
  92.       * file there's no need to detach.
  93.       */
  94.       
  95.      if (getppid() != 1) {
  96.  
  97.       
  98.  
  99. #ifdef SIGTTOU
  100.       signal(SIGTTOU, SIG_IGN);
  101. #endif
  102. #ifdef SIGTTIN
  103.       signal(SIGTTIN, SIG_IGN);
  104. #endif
  105. #ifdef SIGTSTP
  106.       signal(SIGTSTP, SIG_IGN);
  107. #endif
  108.  
  109.       /*
  110.        * If we were not started in the background, fork and let
  111.        * the parent exit.  This also guarantees the first child
  112.        * is not a process group leader.
  113.        */
  114.  
  115.       if ( (childpid = fork()) < 0)
  116.            err_sys("can't fork first child");
  117.       else if (childpid >0)
  118.            exit(0);     /* parent */
  119.       
  120.       /*
  121.        * First Child process
  122.        *
  123.        * Disassociate from controlling terminal and process group.
  124.        * Ensure the process can't reacquire a new controlling terminal
  125.        */
  126.       
  127. #if defined(SIGTSTP) && !defined(USG)  /* BSD */
  128.       
  129. #if defined(hpux) || defined(_SEQUENT_) || defined(_CRAY) || defined(USG)
  130.       setsid();
  131. #else
  132.       if (setpgrp(0, getpid()) == -1)
  133.            err_sys("can't change process group");
  134.       
  135.       if ( (fd = uopen("/dev/tty", O_RDWR)) >= 0) {
  136.            ioctl(fd, TIOCNOTTY, (char *)NULL); /* lose controlling TTY*/
  137.            close(fd);
  138.       }
  139. #endif /* hpux  et al.*/
  140.       
  141. #else /* System V */
  142.       
  143.       if (setpgrp() == -1)
  144.            err_sys("Can't change process group");
  145.       
  146.       signal(SIGHUP, SIG_IGN);  /* immune from pgrp leader death */
  147.       
  148.       if ( (childpid = fork()) < 0)
  149.            err_sys("Can't fork second child");
  150.       else if (childpid > 0)
  151.            exit(0); /* First child */
  152.       
  153.       /* second child */
  154. #endif
  155.  
  156.      }  /* End of if test for ppid == 1 */
  157.  
  158.      /** Close any file descriptors **/
  159.  
  160.      for (fd = 0; fd < NOFILE; fd++)
  161.       close(fd);
  162.  
  163.      errno = 0;  /* probably set to EBADF from a close */
  164.  
  165.  
  166.      /** Change the current directory to / **/
  167.  
  168.      /* No need for this, since we're already where we want to be */
  169.      /* rchdir("/"); */
  170.  
  171.      /** Clear inherited file mode creation mask. **/
  172.  
  173.      umask(0);
  174.  
  175.      /*
  176.       * See if the caller isn't ineterested in the exit status of its
  177.       * children, and doesn't want to have them become zombies
  178.       */
  179.  
  180.      if (ignsigcld) {
  181. #if defined(SIGCLD) && !defined(_CRAY) && !defined(USG)
  182.       void sig_child();
  183.  
  184.       signal(SIGCLD, sig_child);  /* BSD */
  185. #else
  186.       signal(SIGCLD, SIG_IGN);    /* System V */
  187. #endif
  188.      }
  189. }
  190.